package lgvalle.com.fluxtodo.flux.actions;

import lgvalle.com.fluxtodo.flux.fluxbase.Dispatcher;
import lgvalle.com.fluxtodo.model.Todo;

import javax.inject.Inject;

/**
 * Created by lgvalle on 02/08/15.
 */
// 业务逻辑的actions 封装创建帮助类
public class ActionsDispatcherDelegate {
    // 分发器
    final Dispatcher dispatcher;

    // 构造方法 依赖注入 分发器
    @Inject
    public ActionsDispatcherDelegate(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }
    /**
     *
     * 封装的发送事件
     */
    // 通过text创建一个todo
    public void create(String text) {
        dispatcher.dispatch(
                TodoActions.TODO_CREATE,
                TodoActions.KEY_TEXT, text
        );
    }

    // 通过id删除一个todo
    public void destroy(long id) {
        dispatcher.dispatch(
                TodoActions.TODO_DESTROY,
                TodoActions.KEY_ID, id
        );
    }

    // undo 删除的操作
    public void undoDestroy() {
        dispatcher.dispatch(
                TodoActions.TODO_UNDO_DESTROY
        );
    }

    // 切换完成状态
    public void toggleComplete(Todo todo) {
        long id = todo.getId();
        String actionType = todo.isComplete() ? TodoActions.TODO_UNDO_COMPLETE : TodoActions.TODO_COMPLETE;
        dispatcher.dispatch(
                actionType,
                TodoActions.KEY_ID, id
        );
    }

    // 切换所有的状态
    public void toggleCompleteAll() {
        dispatcher.dispatch(TodoActions.TODO_TOGGLE_COMPLETE_ALL);
    }

    // 清除所有已经完成的
    public void destroyCompleted() {
        dispatcher.dispatch(TodoActions.TODO_DESTROY_COMPLETED);
    }
}
